<template>
	<view>
		<view v-for="(item,index) in enterpriseList">
			<view class="enterpriseList" @click="saveEnterpriseInfo(index)">
				<u-avatar :src="item.image" size="20vw"></u-avatar>
				<view class="name">
					{{item.name}}
				</view>
			</view>
		</view>

		<view v-if="show">
			<view class="enterpriseList">
				<u-avatar :src="image" size="20vw" @click="goToImage"></u-avatar>
				<view class="name" @click="editName">
					{{name}}
				</view>
			</view>
			<view class="creat" @click="addEnterprise">
				创建
			</view>
		</view>


		<view class="enterpriseList" @click="show=!show">
			<u-icon name="plus"></u-icon>
		</view>
		
		<view class="enterpriseList" @click="changeVersion1">
			切换到个人版
		</view>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				enterpriseList: [],
				image: "https://web-assets.dcloud.net.cn/unidoc/zh/icon.png?v=1556263038788",
				name: "点击头像及名称进行更改",
				show: false
			};
		},
		onLoad() {
			this.getEnterpriseList();
		},
		methods: {
			goToImage() {
				let that = this;
				uni.chooseImage({
					sourceType: ['album'], //从相册选择
					count: 1,
					success(res) {
						let filePath = res.tempFilePaths[0]
						uniCloud.uploadFile({
							filePath: filePath,
							cloudPath: Math.floor(Math.random() * 900000000) + 100000000 + '.jpg',
							success(res) {
								that.image = res.fileID;
							}
						});
					}
				});
			},
			editName() {
				let that = this;
				uni.showModal({
					title: "请输入要更改的名称：",
					editable: true,
					success: function(res) {
						if (res.confirm) {
							if (res.content == "") {
								uni.showToast({
									icon: "error",
									title: "名称为空"
								})
							} else {
								that.name = res.content;
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			saveEnterpriseInfo(e) {
				uni.setStorageSync("enterprise", this.enterpriseList[e]);
				this.changeVersion();
			},
			addEnterprise() {
				uni.request({
					url: getApp().url + 'enterprise/addEnterprise',
					data: {
						adminId: uni.getStorageSync("userId"),
						name:this.name,
						image:this.image
					},
					header: {
						token: uni.getStorageSync("token")
					},
					method: "POST",
					success: (response) => {
						if (response.data.code == -1) {
							return uni.showToast({
								icon: "error",
								title: response.data.msg
							});
						};
						this.getEnterpriseList();
						this.show = false;
					},
					fail: (error) => {
						console.log(error);
					}
				})
			},
			getEnterpriseList() {
				uni.request({
					url: getApp().url + 'enterprise/getEnterpriseList',
					data: {
						userId: uni.getStorageSync("userId")
					},
					header: {
						token: uni.getStorageSync("token")
					},
					method: "GET",
					success: (response) => {
						if (response.data.code == -1) {
							return uni.showToast({
								icon: "error",
								title: response.data.msg
							});
						};
						this.enterpriseList = response.data.data;
						console.log(this.enterpriseList);
					},
					fail: (error) => {
						console.log(error);
					}
				})
			},
			changeVersion() {
				uni.setTabBarItem({
					index: 0,
					pagePath: "/pages/enterprise/index/index",
					success() {
						uni.switchTab({
							url: '/pages/enterprise/index/index'
						})
					}
				})
				uni.setTabBarItem({
					index: 1,
					pagePath: "/pages/enterprise/contacts/contacts"
				})
				uni.setTabBarItem({
					index: 2,
					pagePath:"/pages/enterprise/staging/staging",
					text:"工作台"
				})
				uni.setTabBarItem({
					index: 3,
					pagePath: "/pages/enterprise/my/my"
				})
			},
			changeVersion1() {
				uni.setStorageSync("enterprise", {
					enterpriseId: 0
				});
				uni.setTabBarItem({
					index: 0,
					pagePath: "/pages/person/index/index",
					success() {
						uni.switchTab({
							url: "/pages/person/index/index"
						})
					}
				})
				uni.setTabBarItem({
					index: 1,
					pagePath: "/pages/person/contacts/contacts"
				})
				uni.setTabBarItem({
					index: 2,
					pagePath:"/pages/person/find/find",
					text:"发现"
				})
				uni.setTabBarItem({
					index: 3,
					pagePath: "/pages/person/my/my"
				})
			}
		}
	}
</script>

<style lang="scss">
	.enterpriseList {
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 20px 5vw;
		padding: 5px 5vw;
		height: 20vw;
		box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;

		.name {
			width: 60vw;
			text-align: center;
			line-height: 20vw;
		}
	}

	.creat {
		margin: 20px 5vw;
		padding: 5px 5vw;
		height: 20vw;
		line-height: 20vw;
		text-align: center;
		box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
	}
</style>